<?php

// +----------------------------------------------------------------------

// | Fanwe 多语商城建站系统 (Build on ThinkPHP)

// +----------------------------------------------------------------------

// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.

// +----------------------------------------------------------------------

// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )

// +----------------------------------------------------------------------

// | Author: awfigq(67877605@qq.com)

// +----------------------------------------------------------------------



// 订单

class OrderAction extends CommonAction{

	public function index()

	{

		$data_name = trim($_REQUEST['data_name']);

		if(!empty($data_name))

		{

			$this->assign("data_name",$data_name);

			$where.=" and o.data_name like '%$data_name%'";

			$parameter .= "data_name=" . urlencode($data_name)."&";

		}

		

		$sn = trim($_REQUEST['sn']);

		if(!empty($sn))

		{

			$this->assign("sn",$sn);

			$where.=" and o.sn like '%$sn%'";

			$parameter .= "sn=" . urlencode($sn)."&";

		}

		

		$user_name = trim($_REQUEST['user_name']);

		if(!empty($user_name))

		{

			$this->assign("user_name",$user_name);

			$where.=" and o.user_name like '%$user_name%'";

			$parameter .= "user_name=" . urlencode($user_name)."&";

		}

		

		$goods_status = trim($_REQUEST['goods_status']);

		if($goods_status != "" && $goods_status >= 0)

		{

			$where.=" and o.goods_status = $goods_status";

			$parameter .= "goods_status=" . $goods_status ."&";

			$this->assign("goods_status",$goods_status);

		}

		else

		{

			$this->assign("goods_status",-1);

		}

		

		$sql_str = 'SELECT o.* '.

					'FROM '.C("DB_PREFIX").'order as o '.

					"where o.id is not null $where group by o.id";

		

		$model = M();

        $voList = $this->_Sql_list($model, $sql_str, "&".$parameter, 'id', false);

        

		$this->display ();

		return;

	}



	public function show()

	{

		$id = intval($_REQUEST['id']);

		$order = D("Order")->where("id = $id")->find();

		$order['goods_status_name'] = L("ORDER_GOODS_STATUS_".$order['goods_status']);

		$this->assign ( 'order', $order );

		

		$goods = D("ExchangeGoods")->where("id = ".$order['rec_id'])->find();

		$this->assign ( 'goods', $goods );

		

		if($goods['goods_type'] < 2)

		{

			$bonds = D("GoodsBond")->where("goods_id = ".$goods['goods_id']." and order_sn = '' and user_id = 0")->findAll();

			$this->assign ( 'bonds', $bonds );

		}

		

		$this->display ('show');			

	}

	

	public function update()

	{

		$id = intval($_REQUEST['id']);

		$status = intval($_REQUEST['status']);

		$order = D("Order")->where("id = $id")->find();

		$goods = D("ExchangeGoods")->where("id = ".$order['rec_id'])->find();

		

		if($goods['goods_type'] < 2 && empty($order['adm_memo']) && $status == 1)

		{

			$bond_id = intval($_REQUEST['groupBond']);

			$bond = D("GoodsBond")->where("id = $bond_id")->find();

			$bond['user_id'] = $order['user_id'];

			$bond['order_sn'] = $order['sn'];

			$bond['send_time'] = gmtTime();

			D("GoodsBond")->save($bond);

			

			$memo = "序列号：".$bond['sn'];

			if(!empty($bond['password']))

			{

				$memo.="<br/>密码：".$bond['password'];

			}

			

			$order['adm_memo'] = $memo;

		}

		else

		{

			$order['adm_memo'] = trim($_REQUEST['adm_memo']);

		}

		

		$order['goods_status'] = $status;

		D("Order")->save($order);

		$this->success (L('EDIT_SUCCESS'));

	}

	

	public function foreverdelete() {

		//删除指定记录

		$name=$this->getActionName();

		$model = D ($name);

		if (! empty ( $model )) {

			$pk = $model->getPk ();

			$id = $_REQUEST [$pk];

			$ids = explode ( ',', $id );

			$names = '';

			foreach($ids as $idd)

			{

				$names .= M("Order")->where("id=".$idd)->getField("sn").",";

			}

			if($names!='')

			{

				$names = substr($names,0,strlen($names)-1);

			}

			if (isset ( $id )) {

				$condition = array ($pk => array ('in', explode ( ',', $id ) ) );

				if (false !== $model->where ( $condition )->delete ()) {

					$msg = '删除定单:'.$names."ID:".$id;

					$this->saveLog(1,0,$msg);

					$this->success (L('DEL_SUCCESS'));

				} else {

					

					$msg = '删除定单:'.$names."ID:".$id;

					$this->saveLog(0,0,$msg);

					$this->error (L('DEL_FAILED'));

				}

			} else {				

				$msg = '删除定单:'.$names;

				$this->saveLog(0,0,$msg);

				$this->error ( L('INVALID_OP') );

			}

		}

		$this->forward ();

	}

}



function getOrderGoodsStatus($status)

{

	return L("ORDER_GOODS_STATUS_".$status);	

}



function getOrderEdit($id,$status)

{

	if($status == 0)

	{

		return "<a href='javascript:showOrder(".$id.");'>". L('_EDIT_DATA')."</a> <a href='javascript:foreverdel(".$id.");'>". L('_DELETE_DATA')."</a>";

	}

	elseif ($status == 1){

		return "<a href='javascript:showOrder(".$id.");'>". L('VIEW')."</a>";

	}

	else{

		return "<a href='javascript:showOrder(".$id.");'>". L('VIEW')."</a> <a href='javascript:foreverdel(".$id.");'>". L('_DELETE_DATA')."</a>";

	}

	

}

?>